package com.ebook.core.util;

import java.util.Map;

import com.ebook.core.domain.Bookmarker;
import com.ebook.core.domain.ReadPosition;

public class BookmarkerUtil {

	/**
	 * 从map中读取内容解析一个笔记书签的基本信息(map一般是查询数据库返回的结果)
	 * @param map
	 * @return book如果正常，否则返回null
	 */

	public static Bookmarker parseBookmarker(Map<String, Object> map) {
		try {		
			Integer uid = (Integer)map.get("uid") == null ? 0 : (Integer)map.get("uid") ;
			Integer bookid = (Integer)map.get("bookid") == null ? 0 : (Integer)map.get("bookid") ;
			String markerid = (String)map.get("markerid") == null ? null : (String)map.get("markerid") ;
			String cfi = (String)map.get("cfi") == null ? "" : (String)map.get("cfi") ;
			Integer curr_chapter = (Integer)map.get("curr_chapter")  == null ? 0 : (Integer)map.get("curr_chapter") ;
			String title = (String)map.get("title") == null ? "" : (String)map.get("title");
			String content = (String)map.get("content") == null ? "" : (String)map.get("content");
			String create_time = (String)map.get("create_time").toString() == null ? "" : map.get("create_time").toString();	
			return new Bookmarker(uid, bookid, markerid, cfi, curr_chapter, title, content, create_time);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}

	public static ReadPosition parseReadPosition(Map<String, Object> map) {
		try {
			Integer uid = (Integer)map.get("uid") == null ? 0 : (Integer)map.get("uid") ;
			Integer bookid = (Integer)map.get("bookid") == null ? 0 : (Integer)map.get("bookid") ;
			Integer readPositionid = (Integer)map.get("readPositionid") == null ? 0 : (Integer)map.get("readPositionid") ;
			String cfi = (String)map.get("cfi") == null ? "" : (String)map.get("cfi") ;
			Integer curr_chapter = (Integer)map.get("curr_chapter")  == null ? 0 : (Integer)map.get("curr_chapter") ;
			String update_time = (String)map.get("update_time").toString()  == null ? "" : (String)map.get("update_time").toString() ;
			return new ReadPosition(uid,bookid,readPositionid,cfi,curr_chapter,update_time);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
		
		
	}
}
